package com.tubi.edu.info.manager.dao;

import com.tubi.edu.info.manager.domain.Student;

// 库管
public class StudentDao implements BaseStudentDao {
    //1。创建一个学习对象数组
    private static Student[] stus = new Student[5];  //前面加static 属于静态成员对象共享
     static {
         Student stu1 = new Student("001","ronglei","21","1991-12-12");
         Student stu2 = new Student("003","rongl3ei","21","1991-12-12");
         Student stu3 = new Student("002","rongle32i","213","1991-12-12");
         stus[0] = stu1;
         stus[1] = stu2;
         stus[2] = stu3;
     }
    // 添加学生
    public boolean addStudent(Student stu) {
        //2。 添加学学生到数组
        //2。1 定义变量index 为-1 假设数组已经存满没有null元素
        int index = -1;
        //2。2 遍历数组取出每一个元素是否为null
        for (int i = 0; i < stus.length; i++) {
            if(stus[i] == null){
                index = i;
                //2。3如果是null 让index 遍历记录当前索引位置，使用break结束遍历
                break;
            }
        }
        //3。返回是否添加成功的布尔值
        if(index == -1) {
            return false;
        }else {
            stus[index] = stu;
            return true;
        }
    }
    //查看学生
    public Student[] findAllStudent() {
        return stus;
    }
    //删除学生
    public void deleteStudentById(String id) {
        //查找id所在数组的位置
        int index = getIndex(id);
        //找出来用null覆盖
        stus[index] = null;

    }
    //查找id所在数组的位置
    public int getIndex ( String id){
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = StudentDao.stus[i];
            if(stu != null && stu.getId().equals(id)){
                index = i;
            }
        }
        return index;
    }
    //修改学生
    public void updateStudent(String id, Student stu) {
        //查找 id位置
        int index = getIndex(id);
        // 进行替换
        stus[index] = stu;
    }
}
